/**
 * 仅用于，能力综合分计算。便于统一修改
 */
import React from 'react';

export default function useAssessTotal(data, form) {
    const { assessAbility = {}, period, teacherEqLeader } = data;
    const { levels, leaderRate, teacherRate } = assessAbility; // 评分等级规则

    const onlyLeader = teacherEqLeader === 1;

    // 本周期评估信息
    const assessInfo = React.useMemo(() => {
        const { assessWeekly, assessMonthly, assessQuarterly } = data;
        const map = {
            weekly: assessWeekly,
            monthly: assessMonthly,
            quarterly: assessQuarterly
        };
        return map[period] || [];
    }, [data]);

    const assessTotal = React.useMemo(() => {
        const { assessSummary = [] } = form.getFieldsValue(); // 本次评估详情
        const { assessTable = {} } = assessInfo[0] || {}; // 本周期历史评分
        const { score: leaderScore, level: leaderLevel = '' } = assessSummary[0] || {}; // 主管评分
        const { score: teacherScore = '' } = assessTable; // 带教人评分

        if (leaderLevel === '' || (teacherScore === '' && !onlyLeader)) {
            return {};
        }

        // 综合能力分 = 主管评分 * leaderRate/100 + 带教人评分 * teacherRate/100 ，如果主管带教人为同一人，则只取主管的分数。
        const finalScore = onlyLeader
            ? Number(leaderScore)
            : (Number(teacherScore) * teacherRate + Number(leaderScore) * leaderRate) / 100;
        // 评估等级
        const levelItem =
            levels.find(
                ({ min, max }) => (Number(min) < finalScore || Number(min) === 0) && finalScore <= Number(max)
            ) || {};

        return {
            score: finalScore.toFixed(2),
            level: levelItem.level
        };
    }, [form, levels]);

    return assessTotal;
}
